class Array
    def swap!(a,b)
         self[a], self[b] = self[b], self[a]
    self
    end
end

class Gauss

	def initialize(matrix, dim)
		@pivot = 0
		@vector = Array.new()
		@tab = matrix
		@dim = dim

	end

################################################

	def display
		@tab.length.times do |i|
			@tab[i].length.times do |j|
				print "| "+@tab[i][j].to_s + " | "
			end
		puts ""
		end
	end

################################################

	def displayVector
		puts " Vecteur solution :"
		@tab.length.times do |i|
			@tab[i].length.times do |j|
				if @dim == j
          res = eval '%.5f' % @tab[i][j]
					puts "\t" + res.to_s
				end
			end
		end
	end
  
################################################

	def switchLine
		@tab.length.times do |i|
			if @tab[i][@pivot] != 0
				@tab.swap!(@pivot, i)
				return true			
			end
		end
		return false
	end

################################################

	def	calcLine(op)
		@tab[@pivot].length.times do |i|
			@tab[@pivot][i] = op * @tab[@pivot][i]
		end
	end

################################################

	def	calcRow
		@tab.length.times do |i|
			if i != @pivot
				coef = @tab[i][@pivot]
				@tab[i].length.times do |j|
					@tab[i][j] = -@tab[@pivot][j] * coef + @tab[i][j]
				end		
			end
		end
	end

################################################

def error(error)
    lol = Array.new
    error.length.times do |i|
      lol[i] = error[i].pop
    end
    puts "\nMarge d'erreur:"
    error.length.times do |i|
      tmp = 0
      error[i].length.times do |j|
        tmp += error[i][j] * @tab[j].last
      end
      puts "\t"+ (tmp - lol[i]).to_s
    end
  end

################################################

	def pivot
		i = 0
		while @dim > i
			if @tab[@pivot][@pivot] == 0
				if !switchLine
					puts "Unable to find a no null value"
					break
				end				
			end
			if @tab[@pivot][@pivot] != 1
         calcLine(1.0/@tab[@pivot][@pivot])
			end
			calcRow
			@pivot += 1
			i += 1
		end
	end
end
